import java.util.*;
import java.awt.*;

import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.util.*;

@ScriptManifest(
		authors = { "Absox" },
		category = "Money",
		name = "TarGather",
		version = .13,
		
		description = "<html><head><style type='text/css'>" +
				"body {font-family: Arial, Helvetica, sans-serif}" +
				"p {size: 12px}" +
				"</style></head>" +
				"<body><center><h2>TarGather v" + .13 + "</h2>" +
				"<p>By Absox</p></center>" +
				"<p>Script Description:" +
				"<ul>" +
				"<li>Start near any swamp tar spawn, making sure the swamp tar is visible.</li>" +
				"<li>Run the script, sit back, and relax.</li>" +
				"</ul></p></body></html>"
		)

public class TarGather extends Script implements PaintListener {
	
	public long startTime;
	
	public String state;
	public String status = "Starting...";
	
	public boolean StartedY;
	
	public int swampTarID = 1939;
	public int startCount = -1;
	public int gotCount;
	public int tarPrice;
	public int profit;
	
	public boolean isWelcomeButton() {
		final RSInterface welcomeInterface = RSInterface.getInterface(378);
		if (welcomeInterface.getChild(45).getAbsoluteX() > 20
				|| !welcomeInterface.getChild(117).getText().equals(
						"10.1120.190")
				&& !welcomeInterface.getChild(117).getText().equals("")) {
			log("We still are in Welcome Screen");
			return true;
		} else {
			return false;
		}
	}
	
	public void getTar() {
		
		RSItemTile tarTile;
		int distance;
		RSTile myLocation = getMyPlayer().getLocation();
		
		tarTile = getNearestGroundItemByID(new int[] { swampTarID });
		distance = distanceTo(tarTile);
		
		if(distance > 15) {
			RSTile midway = new RSTile(
					(tarTile.getX() + myLocation.getX()) / 2,
					(tarTile.getY() + myLocation.getY()) / 2
			);
			
			walkTileMM(randomizeTile(midway, 2, 2));
			wait(distanceTo(midway) * 300 + random (-200, 200));
			walkTileMM(randomizeTile(tarTile, 2, 2));
			wait(distanceTo(midway) * 300 + random (-200, 200));
			
		} else if (distance >= 5) {
			walkTileMM(randomizeTile(tarTile, 2, 2));
			wait(distance * 300 + random (-200, 200));
		}
		
		wait(random(200, 500));
			
		atTile(tarTile, "Take");
		wait(random(500, 1200));
		
		return;
		
	}
	
	public boolean onStart(Map<String, String> args) {
		tarPrice = grandExchange.loadItemInfo(swampTarID).getMarketPrice();
		return true;
	}
	
	public void onFinish() {
		ScreenshotUtil.takeScreenshot(isLoggedIn());
	}
	
	public void onRepaint(Graphics g) {
		
		Color BG = new Color (50, 50, 50, 200);
		int x = 10;
        int y = 4;
        
        if (startTime == 0) {
			 startTime = System.currentTimeMillis();
        }
        
        long millis = System.currentTimeMillis() - startTime;
		long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		long seconds = millis / 1000;
        
		gotCount = getInventoryCount(swampTarID) - startCount;
		profit = gotCount * tarPrice;
		
		g.setColor(BG);
		g.fill3DRect(x-6, y, 250, 150, true);
		
		g.setColor(Color.cyan);
		g.setFont(new Font("Verdana", Font.BOLD, 14));
		g.drawString("TarGather v" + getClass().getAnnotation(ScriptManifest.class).version(), x, y+=15);
		g.setFont(new Font("Verdana", Font.PLAIN, 12));
		g.drawString("Runtime: " + hours + ":" + minutes + ":" + seconds, x, y+=15);
		g.drawString("Status: " + status, x, y+=15);
		g.drawString("Collected: " + gotCount, x, y+=15);
		g.drawString("Profit: " + profit + " gp", x, y+=15);
		
	}
	
	public int loop() {
		
		if (!isLoggedIn() || isWelcomeButton() || !StartedY) {
			StartedY = true;
			wait(1000);
			return random(250, 500);
		}
		
		setCameraAltitude(true);
		
		if (getEnergy() >= 50 + random(-10, 10)) {
			setRun(true);
		}
		
		while (getGroundItemByID(swampTarID) == null) {
			wait(1000 + random(-200, 200));
		}
		
		if (state == null) {
			state = "run";
		}
		
		if (startCount == -1) {
			startCount = getInventoryCount(swampTarID);
		}
		
		if (state == "run") {
			status = "Running";
			getTar();
		}
		
		return random(100, 150);
	}
	
}